<template>
  <div id="category">
    <div class="control_button">
      <el-button type="primary" round plain @click="isShow.flag = true"
        >新增</el-button
      >
    </div>
    <el-table
      :data="cateList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column prop="catename" label="分类名称"> </el-table-column>
      <el-table-column prop="img" label="分类图片">
        <template v-slot="$props">
          <img :src="$url + $props.row.img" alt="" style="width: 60px" />
        </template>
      </el-table-column>
      <el-table-column prop="status" label="分类状态">
        <template v-slot="$props">
          <el-button
            type="danger"
            round
            plain
            size="mini"
            v-if="$props.row.status == 1"
            >正常</el-button
          >
          <el-button type="warning" round plain size="mini" v-else
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template v-slot="$pops">
          <el-button
            type="warning"
            round
            plain
            size="mini"
            @click="setData($pops.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            round
            plain
            size="mini"
            @click="delCate($pops.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Dialog
      :isShow="isShow"
      :cateList="cateList"
      @reload="reload"
      ref="dialog_ref"
    />
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import Dialog from "../components/addCate.vue";
export default {
  data() {
    return {
      isShow: { flag: false },
    };
  },
  computed: {
    ...mapState("cate_model", ["cateList"]),
  },
  methods: {
    delCate(id) {
      this.$axios.post("/catedelete", { id }).then((res) => {
        if (res.code === 200) {
          this.$message({
            type: "success",
            message: "删除成功",
          }),
            this.reload();
        }
      });
    },
    reload() {
      this.cateListActions();
    },
    setData(obj) {
      this.$refs.dialog_ref.setData(obj);
      this.isShow.flag = true;
    },
    ...mapActions("cate_model", ["cateListActions"]),
  },
  created() {
    this.cateListActions();
    console.log(this.$store.state.cate_model);
  },
  components: {
    Dialog,
  },
};
</script>

<style>

</style>